package com.finalist.template.directive;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.finalist.entity.Product;
import com.finalist.entity.Product.RecommendType;
import com.finalist.service.ProductService;
import com.finalist.util.FreemarkerUtils;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 关联推荐
 * @author wangxm
 *
 */
@Component("productRecommendListDirective")
public class ProductRecommendListDirective extends BaseDirective {

	/** "商品ID"参数名称 */
	private static final String PRODUCT_RECOMMEND_ID = "id";

	/** "商品关联类型"参数名称 */
	private static final String PRODUCT_RECOMMEND_RECOMMENDTYE = "recommendType";

	/** "推荐商品数"参数名称 */
	private static final String PROMOTION_RECOMMEND_COUNT = "count";
	
	/** "是否合约机"参数名称 */
	private static final String PROMOTION_HASAGREEMENT = "hasAgreement";
	
	
	@Resource(name = "productServiceImpl")
	private ProductService productService;
	
	/** 变量名称 */
	private static final String VARIABLE_NAME = "products";
	
	/* (non-Javadoc)
	 * @see freemarker.template.TemplateDirectiveModel#execute(freemarker.core.Environment, java.util.Map, freemarker.template.TemplateModel[], freemarker.template.TemplateDirectiveBody)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		Long id = FreemarkerUtils.getParameter(PRODUCT_RECOMMEND_ID, Long.class, params);
		RecommendType recommendType = FreemarkerUtils.getParameter(PRODUCT_RECOMMEND_RECOMMENDTYE, RecommendType.class, params);
		Integer count = FreemarkerUtils.getParameter(PROMOTION_RECOMMEND_COUNT, Integer.class, params);
		Boolean hasAgreement = FreemarkerUtils.getParameter(PROMOTION_HASAGREEMENT, Boolean.class, params);
		
		
		List<Product> recommendlist = productService.recommendlist(id, hasAgreement, recommendType, count);
//		List<Product> recommendlist = productService.findAll();
		setLocalVariable(VARIABLE_NAME, recommendlist, env, body);
	}
	
	
}
